/**
 * @(#)BuyerAjaxController.java, 2018/2/6.
 * <p/>
 * Copyright 2018 Kerr Wang, Inc. All rights reserved.
 * KERRWANG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package org.wangzx.exercise.controller.xhr;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wangzx.exercise.enums.BuyStateType;
import org.wangzx.exercise.meta.bean.OrderCartBean;
import org.wangzx.exercise.meta.bean.UserBean;
import org.wangzx.exercise.meta.vo.CartVO;
import org.wangzx.exercise.meta.vo.ItemVO;
import org.wangzx.exercise.meta.vo.UserVO;
import org.wangzx.exercise.meta.vo.common.AjaxResult;
import org.wangzx.exercise.meta.vo.common.ResponseCode;
import org.wangzx.exercise.service.basic.ItemService;
import org.wangzx.exercise.service.basic.OrderCartService;
import org.wangzx.exercise.service.core.OrderCartCoreService;
import org.wangzx.exercise.service.core.UserCoreService;
import org.wangzx.exercise.utils.LoginUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author hzwangzuxiang(ahscwzx@163.com)
 */
@Controller
@RequestMapping("/")
public class BuyerAjaxController extends BaseAjaxController {
    static private final Logger logger = LoggerFactory.getLogger(BuyerAjaxController.class);

    @Autowired
    private OrderCartService orderCartService;

    @Autowired
    private UserCoreService userCoreService;

    @Autowired
    private OrderCartCoreService orderCartCoreService;

    @Autowired
    private ItemService itemService;

    @RequestMapping("/addCart.json")
    @ResponseBody
    public AjaxResult addCart(@RequestParam(name="id") long itemId, @RequestParam(name="count") int count, HttpServletRequest request) {
        logger.info("[op:addCart] itemId={}, count={}", itemId, count);

        UserVO userVO = LoginUtils.loginCheck(request);
        logger.info("[op:addCart] userVO={}", JSON.toJSONString(userVO));

        if (userVO.getLoginType() != 1) {
            return initFailureResult(ResponseCode.UNAUTHORIZED);
        }
        UserBean userBean = userCoreService.getByName(userVO.getUserName());
        if (userBean == null) {
            return initFailureResult(ResponseCode.UNAUTHORIZED);
        }

        ItemVO itemVO = itemService.getById(itemId, userVO);
        if(itemVO == null) {
            return initFailureResult(ResponseCode.FAIL);
        }

        if (count <= 0) {
            return initSuccessResult(false);
        }

        itemVO.setCount(count);
        long userId = userBean.getId();
        OrderCartBean orderCartBean = orderCartCoreService.getByBuyerAndItemId(userId, itemVO.getId());
        if (orderCartBean != null) {
            return initFailureResult(ResponseCode.FAIL);
        }
        orderCartService.insert(itemVO, userId);
        return initSuccessResult(true);
    }

    @RequestMapping("/addOrder.json")
    @ResponseBody
    public AjaxResult addOrder(@RequestBody List<CartVO> carts, HttpServletRequest request) {
        logger.info("[op:addOrder] carts={}", JSON.toJSONString(carts));

        UserVO userVO = LoginUtils.loginCheck(request);

        if(userVO.getLoginType() != BuyStateType.BUY.getValue()) {
            return initFailureResult(ResponseCode.UNAUTHORIZED);
        }

        if(!CollectionUtils.isNotEmpty(carts)) {
            return initSuccessResult(false);
        }

        long userId = userCoreService.getByName(userVO.getUserName()).getId();

        boolean res = orderCartService.addOrder(carts, userId);

        return initSuccessResult(true);
    }
}
